package fr.castorhoz.isi_connectivity_widget.bluetooth_widget;

import java.util.ArrayList;
import java.util.List;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import fr.castorhoz.isi_connectivity_widget.ConnBroadcastReceiver;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.database.DBAdapter;

public class BluetoothBroadcastReceiver extends ConnBroadcastReceiver {

	private static List<Integer> appWidgetIds = new ArrayList<Integer>();
	
	private DBAdapter dbAdapter;
	
	public BluetoothBroadcastReceiver(Context context) { //ConnButton button) {
		super(context); //button);
		dbAdapter = new DBAdapter(context);
		dbAdapter.open();
		appWidgetIds = dbAdapter.getAppWidgetIdUsingWidget(ConnButton.widgets.gpsWidget.name());
		dbAdapter.close();
	}
	
	/**
	 * Adds the appWidget to the ones that need to be notified when the broadcast receiver receives an event
	 * @param appWidgetId
	 */
	public static void addAppWidget(int appWidgetId) {
		Log.d(BluetoothBroadcastReceiver.class.getName(), "adding appWidgetId "+appWidgetId+" to BluetoothBroadcastReceiver");
		appWidgetIds.add(appWidgetId);
	}

	@Override
	public void doOnReceive(Context context, Intent intent) {
		/*final String action = intent.getAction();

		if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
			Log.i(this.getClass().toString(), "bluetooth changed");

			int info = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);

			// METTRE A JOUR LES IMAGES ET LES ETATS DES COMPONENTS
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);
			int selector = button.getSelectorUnknown();

			if (info == (BluetoothAdapter.STATE_ON)) {
				Log.i(this.getClass().toString(), "Status of bluetooth : ON");
				Toast.makeText(context.getApplicationContext(),
						"Status of bluetooth : ON", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorOn();
				button.setState(ConnButton.STATE_ON);
			} else if (info == (BluetoothAdapter.STATE_TURNING_ON)) {
				Log.i(this.getClass().toString(),
						"Status of bluetooth : turning on");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : turning on", Toast.LENGTH_SHORT)
						.show();
				selector = WifiButton.selector_on;
				button.setState(ConnButton.STATE_ON);
			} else if (info == (BluetoothAdapter.STATE_OFF)) {
				Log.i(this.getClass().toString(), "Status of bluetooth : OFF");
				Toast.makeText(context.getApplicationContext(),
						"Status of bluetooth : OFF", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorOff();
				button.setState(ConnButton.STATE_OFF);
			} else if (info == (BluetoothAdapter.STATE_TURNING_OFF)) {
				Log.i(this.getClass().toString(),
						"Status of bluetooth : turning off");
				Toast.makeText(context.getApplicationContext(),
						"Status of bluetooth : turning off", Toast.LENGTH_SHORT)
						.show();
				selector = button.getSelectorOff();
			}
			//remoteViews.setImageViewResource(button.getIdButton(), selector);
			remoteViews.setImageViewResource(R.id.imageButtonWidgetRow, selector);

		}*/
	}

	@Override
	protected List<Integer> getAppWidgetIds() {
		return appWidgetIds;
	}

//	@Override
//	protected void registerItself(Context context) {
//		/*IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
//		context.getApplicationContext().registerReceiver(this, filter);
//		Log.d(this.getClass().getName(), "broadcast receiver "+this.getClass().getCanonicalName()+" has registered itself");*/
//	}

	@Override
	public String getBroadcastAction() {
		return BluetoothAdapter.ACTION_STATE_CHANGED;
	}

	@Override
	protected String getWidgetName() {
		return ConnButton.widgets.bluetoohWidget.name();
	}
}
